import { Component, OnInit } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { GridConfigService } from 'app/core/config/grid-config.service';
import { GridOptions } from '@ag-grid-community/core';
import {ApesMessageService} from "@shared/apes/message";

@Component({
  selector: 'service-rate-personal',
  template: `
    <div>
      <ag-grid-angular #grid
                       style="width: 100%;" [ngStyle]="{ height: height }" class="ag-theme-balham"
                       enableColResize="true"
                       enableSorting="true"
                       enableFilter="true"

                       suppressLoadingOverlay="true"

                       [rowData]="rowData"
                       [gridOptions]="gridOptions"
                       [columnDefs]="columnDefs"
                       [pinnedBottomRowData]="pinnedBottomRowData">
      </ag-grid-angular>
    </div>
  `,
})
export class ServiceRatePersonal implements OnInit {
  /** Grid 相关 **/
  rowData: any[] = [];
  resultData: any;
  gridOptions: GridOptions;
  pinnedBottomRowData: any[] = [];
  columnDefs: any[] = [];
  height: any = window.innerHeight - 335 + 'px';

  constructor(private apiService: ApiService,
              private gridConfigService: GridConfigService,
              private msg: ApesMessageService) {
    this.columnDefs = [
      {"headerName": "接车人", field: 'JIECRMC', width: 100, pinned: 'left'},
      { 'headerName': '门店', field: 'MDMC', width: 150, pinned: 'left' },
      { 'headerName': '销售组', field: 'XSZMC' },
      { 'headerName': '待跟踪服务单', field: 'WAITTRACK' },
      { 'headerName': '待跟踪大单', field: 'BIGORDER' },
      { 'headerName': '未确认大单', field: 'UNSUREBIGORDER' },
      { 'headerName': '未收款大单', field: 'UNCOLLECTEDBIGORDER' },
      { 'headerName': '已收款大单', field: 'COLLECTEDBIGORDER' },
      { 'headerName': '已终止大单', field: 'CLOSEDBIGORDER' },
      { 'headerName': '大单成交率', field: 'BIGORDERRATE' },
      { 'headerName': '待跟踪小单', field: 'SMALLORDER' },
      { 'headerName': '未收款小单', field: 'UNCOLLECTEDSMALLORDER' },
      { 'headerName': '已收款小单', field: 'COLLECTEDSMALLORDER' },
      { 'headerName': '小单成交率', field: 'SMALLORDERRATE' },
    ];

    // 初始化 grid
    this.gridOptions = <GridOptions>{
      toolPanelSuppressSideButtons: true,
      suppressExcelExport: true,
      suppressCsvExport: true,
      suppressSizeToFit: true,
      showToolPanel: false,
      pagination: true,
      paginationPageSize: 100,
      rowHeight: 35,
    };
    this.gridOptions.localeText = gridConfigService.getLocaleText();
  }

  ngOnInit() {
  }

  getStore(startTime:Date, endTime:Date, timeValue: number, ngNXMD:string, jcrName:string, num:number, money: number, sale: any[], companyId: any[]){
    this.getTablesData(startTime, endTime, timeValue, ngNXMD, jcrName, num, money, sale, companyId);
  }

  getTablesData(startTime:Date, endTime:Date, timeValue: number, ngNXMD:string, jcrName:string, num:number, money: number, sale: any[], companyId: any[]) {
    let data = { 'interfaceMethod': 'ipmMgzzForAppService.getCJL', 'pdata': {
        'STARTTIME': startTime, 'ENDTIME': endTime, 'QRSJ': timeValue, 'MDID': ngNXMD, 'JIECR': jcrName, 'QFBJ': num,
        'DDJE': money, 'XSZID': sale, 'companyId': companyId
      } };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            for (let i = 0, len = data.length; i < len; i++) {
              data[i]['BIGORDERRATE'] = data[i]['BIGORDERRATE'] + '%';
              data[i]['SMALLORDERRATE'] = data[i]['SMALLORDERRATE'] + '%';
            }
            this.rowData = data;
            if( this.rowData.length === 0 ) {
              this.msg.info(`未找到满足条件的数据`);
            }
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }


}
